package com.project.zhuzhou.news;

import java.util.List;

import com.example.zhuzhou.R;
import com.lidroid.xutils.BitmapUtils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewsListBaseAdapter extends BaseAdapter {

	private List<NewsList> list;
	private Context activity;

	public NewsListBaseAdapter(Context activity, List<NewsList> list) {
		super();
		this.list = list;
		this.activity = activity;
	}

	class ViewHolder {
		TextView title;
		TextView source;
		TextView views;
		TextView commentNum;
		TextView dateline;
//		TextView goodNum;
//		TextView badNum;
		ImageView newsImg;
		ImageView newsImg1;
		ImageView newsImg2;
		ImageView newsImg3;

	}

	@Override
	public int getItemViewType(int position) {
		return list.get(position).getType();
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BitmapUtils bitmapUtils = new BitmapUtils(activity);
		int viewType = this.getItemViewType(position);
		if (viewType == 2) {
			final ViewHolder ViewHolder;
			if (convertView == null) {
				ViewHolder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(activity);
				convertView = inflater.inflate(R.layout.newslist_item_threeimg,
						null);
				ViewHolder.title = (TextView) convertView
						.findViewById(R.id.title);
				ViewHolder.newsImg1 = (ImageView) convertView
						.findViewById(R.id.newsImg1);
				ViewHolder.newsImg2 = (ImageView) convertView
						.findViewById(R.id.newsImg2);
				ViewHolder.newsImg3 = (ImageView) convertView
						.findViewById(R.id.newsImg3);
				ViewHolder.source = (TextView) convertView
						.findViewById(R.id.source);
//				ViewHolder.badNum = (TextView) convertView
//						.findViewById(R.id.badNum);
//				ViewHolder.goodNum = (TextView) convertView
//						.findViewById(R.id.goodNum);
				ViewHolder.commentNum = (TextView) convertView
						.findViewById(R.id.commentNum);
				ViewHolder.dateline = (TextView) convertView
						.findViewById(R.id.dateline);
				ViewHolder.views = (TextView) convertView
						.findViewById(R.id.views);
				convertView.setTag(ViewHolder);
			} else {
				ViewHolder = (ViewHolder) convertView.getTag();
			}
			ViewHolder.title.setText(list.get(position).getTitle());
			// ViewHolder.newsImg1.setBackgroundResource
			// (list.get(position).getNewsImg1());
			// ViewHolder.newsImg2.setBackgroundResource
			// (list.get(position).getNewsImg2());
			// ViewHolder.newsImg3.setBackgroundResource
			// (list.get(position).getNewsImg3());
			bitmapUtils.display(ViewHolder.newsImg1, list.get(position)
					.getImagesChild(0));
			bitmapUtils.display(ViewHolder.newsImg2, list.get(position)
					.getImagesChild(1));
			bitmapUtils.display(ViewHolder.newsImg3, list.get(position)
					.getImagesChild(2));
//			ViewHolder.badNum.setText(list.get(position).getBadNum());
//			ViewHolder.goodNum.setText(list.get(position).getGoodNum());
			ViewHolder.source.setText(list.get(position).getSource());
			ViewHolder.commentNum.setText(list.get(position).getCommentNum());
			ViewHolder.dateline.setText(list.get(position).getAboutTime());
			ViewHolder.views.setText(list.get(position).getViews());

			return convertView;

		} else if (viewType == 1) {
			final ViewHolder ViewHolder;
			if (convertView == null) {
				ViewHolder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(activity);
				convertView = inflater.inflate(R.layout.newslist_item_oneimg,
						null);
				ViewHolder.title = (TextView) convertView
						.findViewById(R.id.title);
				ViewHolder.newsImg = (ImageView) convertView
						.findViewById(R.id.newsImg);
				ViewHolder.source = (TextView) convertView
						.findViewById(R.id.source);
//				ViewHolder.badNum = (TextView) convertView
//						.findViewById(R.id.badNum);
//				ViewHolder.goodNum = (TextView) convertView
//						.findViewById(R.id.goodNum);
				ViewHolder.commentNum = (TextView) convertView
						.findViewById(R.id.commentNum);
				ViewHolder.dateline = (TextView) convertView
						.findViewById(R.id.dateline);
				ViewHolder.views = (TextView) convertView
						.findViewById(R.id.views);
				convertView.setTag(ViewHolder);
			} else {
				ViewHolder = (ViewHolder) convertView.getTag();
			}
			ViewHolder.title.setText(list.get(position).getTitle());
			// ViewHolder.newsImg.setBackgroundResource
			// (list.get(position).getNewsImg());
			bitmapUtils.display(ViewHolder.newsImg, list.get(position)
					.getImagesChild(0));
//			ViewHolder.badNum.setText(list.get(position).getBadNum());
//			ViewHolder.goodNum.setText(list.get(position).getGoodNum());
			ViewHolder.source.setText(list.get(position).getSource());
			ViewHolder.commentNum.setText(list.get(position).getCommentNum());
			ViewHolder.dateline.setText(list.get(position).getAboutTime());
			ViewHolder.views.setText(list.get(position).getViews());

			return convertView;
		} else {
			final ViewHolder ViewHolder;
			if (convertView == null) {
				ViewHolder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(activity);
				convertView = inflater.inflate(R.layout.newslist_item_noimg,
						null);
				ViewHolder.title = (TextView) convertView
						.findViewById(R.id.title);
				ViewHolder.source = (TextView) convertView
						.findViewById(R.id.source);
//				ViewHolder.badNum = (TextView) convertView
//						.findViewById(R.id.badNum);
//				ViewHolder.goodNum = (TextView) convertView
//						.findViewById(R.id.goodNum);
				ViewHolder.commentNum = (TextView) convertView
						.findViewById(R.id.commentNum);
				ViewHolder.dateline = (TextView) convertView
						.findViewById(R.id.dateline);
				ViewHolder.views = (TextView) convertView
						.findViewById(R.id.views);
				convertView.setTag(ViewHolder);
			} else {
				ViewHolder = (ViewHolder) convertView.getTag();
			}
			ViewHolder.title.setText(list.get(position).getTitle());
//			ViewHolder.badNum.setText(list.get(position).getBadNum());
//			ViewHolder.goodNum.setText(list.get(position).getGoodNum());
			ViewHolder.source.setText(list.get(position).getSource());
			ViewHolder.commentNum.setText(list.get(position).getCommentNum());
			ViewHolder.dateline.setText(list.get(position).getAboutTime());
			ViewHolder.views.setText(list.get(position).getViews());

			return convertView;
		}

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return getNewsID(position);
	}

	@Override
	public long getItemId(int position) {
		return getNewsID(position);
	}

	public int getNewsID(int position) {
		return list.get(position).getNewsID();
	}
}
